package edu.gatech.mobile.scene.ui.image;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

/**
 * This class puts a canvas on top of image to enable users drawing on top.
 * It tracks the touch events.
 * @author Jing
 *
 */

public class DrawingOverlay extends View {
    
    
    private Bitmap  mBitmap;
    private Canvas  mCanvas;
    private Path    mPath;
    private Paint   mBitmapPaint;
    private Paint       mPaint;
	private Bitmap labelImage;
	private Canvas labelCanvas;
	private boolean labelSelected = false;
	private Context mContext;
	
	
	int[] minMaxLoc;
	
    public DrawingOverlay(Context c, Paint paint, int width, int height, Bitmap bitMap, Bitmap labelImage) {
        super(c);
        
        this.mContext = c;
        //mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        this.mBitmap = bitMap;
        mCanvas = new Canvas(mBitmap);
        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);
        mPaint = paint;
        // mCanvas.drawPaint(mPaint);
        // mPaint.setColor(0x0000FFFF);
        this.labelImage = labelImage;
        labelCanvas = new Canvas( this.labelImage);
     }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }
    
    @Override 
    protected void onDraw(Canvas canvas) {
        //canvas.drawColor(0xFFAAAAAA);
        
    	canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
        canvas.drawBitmap(labelImage, 0,0, mBitmapPaint);
    	
        canvas.drawPath(mPath, mPaint);
        labelCanvas.drawPath(mPath, mPaint);
        
    }
    
    private float mX, mY;
    private static final float TOUCH_TOLERANCE = 4;
    
    public void setNewBitmap(Bitmap bitmap){
    	labelCanvas.setBitmap(bitmap);
    }
    
    /**
     * remember the touch event starting location
     * @param x
     * @param y
     */
    private void touch_start(float x, float y) {
        mPath.reset();
        mPath.moveTo(x, y);
        mX = x;
        mY = y;
    }
    
    /**
     * keep updating the current location as user move
     * @param x
     * @param y
     */
    private void touch_move(float x, float y) {
        float dx = Math.abs(x - mX);
        float dy = Math.abs(y - mY);
        if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
            mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
            mX = x;
            mY = y;
        }
        int xLoc = (int) x;
        int yLoc = (int) y;
        
        //we only care about the events inside the frame
        if(xLoc<minMaxLoc[0] && xLoc>0){
        	minMaxLoc[0] = xLoc;
        }
        
        if(yLoc<minMaxLoc[1] && yLoc>0){
        	minMaxLoc[1] = yLoc;
        }
        if(xLoc>minMaxLoc[2] && xLoc>0){
        	minMaxLoc[2] = xLoc;
        }
        
        if(yLoc>minMaxLoc[3] && yLoc>0){
        	minMaxLoc[3] = yLoc;
        }
   
    }
    
    /**
     * When user lifts up finger signifies the end of touch event and the path is drawn
     */
    private void touch_up() {
        mPath.lineTo(mX, mY);
        // commit the path to our offscreen
        //mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));

        mCanvas.drawPath(mPath, mPaint);
        labelCanvas.drawPath(mPath, mPaint);
        //mPaint.setXfermode(null);
        // kill this so we don't double draw
        mPath.reset();
    }
    
    public void setMinMax(int[] minMax){
    	this.minMaxLoc = minMax;
    }
    
    /**
     * Handles touch events
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
    	
    	if(!this.labelSelected){
    		Toast.makeText(this.mContext, "Please select a label before annotating", Toast.LENGTH_LONG).show();
    		return false;
    	}
    	
        float x = event.getX();
        float y = event.getY();
        
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                touch_start(x, y);
                invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
                touch_move(x, y);
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                touch_up();
                invalidate();
                break;
        }
        return true;
    }

	public boolean isLabelSelected() {
		return labelSelected;
	}

	public void setLabelSelected(boolean labelSelected) {
		this.labelSelected = labelSelected;
	}

    

}